package com.pactera.integral.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtil {

	private static Log log = LogFactory.getLog(DateUtil.class);

	public static String[] getThisQuarter() {
		SimpleDateFormat df = new SimpleDateFormat("MM");
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
		startCalendar.set(Calendar.DAY_OF_MONTH, 1);
		setMinTime(startCalendar);

		Calendar endCalendar = Calendar.getInstance();
		endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
		endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		setMinTime(endCalendar);
		df.format(startCalendar.getTime());
		df.format(endCalendar.getTime());
		String[] month = new String[2];
		month[0] = df.format(startCalendar.getTime());
		month[1] = df.format(endCalendar.getTime());

		return month;
	}

	public static String getThisYear() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, ((int) calendar.get(Calendar.MONTH) / 3) * 3);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		setMinTime(calendar);

		return df.format(calendar.getTime());
	}

	private static void setMinTime(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}

	/**
	 * 
	 * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
	 * 
	 * @param date
	 * @return
	 */
	public static int getSeason(Date date) {

		int season = 0;

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH);
		switch (month) {
		case Calendar.JANUARY:
		case Calendar.FEBRUARY:
		case Calendar.MARCH:
			season = 1;
			break;
		case Calendar.APRIL:
		case Calendar.MAY:
		case Calendar.JUNE:
			season = 2;
			break;
		case Calendar.JULY:
		case Calendar.AUGUST:
		case Calendar.SEPTEMBER:
			season = 3;
			break;
		case Calendar.OCTOBER:
		case Calendar.NOVEMBER:
		case Calendar.DECEMBER:
			season = 4;
			break;
		default:
			break;
		}
		return season;
	}

	/**
	 * scoreType(0为上年度，1为上季度，2为本季度，3为本月度)
	 * @param scoreType
	 * @return
	 */
	public static List<Integer> addMonths(Integer scoreType) {
		Calendar cal = Calendar.getInstance();
		int currentMonth = cal.get(Calendar.MONTH) + 1;

		List<Integer> scoreMonths = new ArrayList<Integer>();
		if (scoreType == 0) {
			for (int i = 1; i <= 12; i++) {
				scoreMonths.add(i);
			}
		} else if (scoreType == 1 || scoreType == 2) {
			Date date = Calendar.getInstance().getTime();
			int season = getSeason(date);
			//上一季度
			if (scoreType == 1) {
				season = (season == 1) ? 4 : season - 1;
			}
			if (season == 1) {
				scoreMonths.add(1);
				scoreMonths.add(2);
				scoreMonths.add(3);
			} else if (season == 2) {
				scoreMonths.add(4);
				scoreMonths.add(5);
				scoreMonths.add(6);
			} else if (season == 3) {
				scoreMonths.add(7);
				scoreMonths.add(8);
				scoreMonths.add(9);
			} else if (season == 4) {
				scoreMonths.add(10);
				scoreMonths.add(11);
				scoreMonths.add(12);
			}
		} else if (scoreType == 3) {
			scoreMonths.add(currentMonth);
		} else {
			log.error("Entry error : Data incorrect!");
		}
		return scoreMonths;
	}
}
